-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenCleanupService.java
More file actions
39 lines (31 loc) · 1.42 KB
/
TokenCleanupService.java
File metadata and controls
39 lines (31 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.catfactsdaily.service;
import com.example.catfactsdaily.entity.TokenBlacklisted;
import com.example.catfactsdaily.repository.TokenBlacklistedRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service
public class TokenCleanupService {
private static final Logger logger = LoggerFactory.getLogger(TokenCleanupService.class);
private final TokenBlacklistedRepository tokenBlacklistedRepository;
@Autowired
public TokenCleanupService(TokenBlacklistedRepository tokenBlacklistedRepository) {
this.tokenBlacklistedRepository = tokenBlacklistedRepository;
}
@Transactional(isolation = Isolation.SERIALIZABLE)
@Scheduled(fixedRate = 420000) // every 7 min
public void removeExpiredTokens() {
Date now = new Date();
List<TokenBlacklisted> expiredTokens = tokenBlacklistedRepository.findByExpirationDateBefore(now);
if (!expiredTokens.isEmpty()) {
tokenBlacklistedRepository.deleteAll(expiredTokens);
logger.info("Expired tokens removed: {}", expiredTokens.size());
}
}
}